<html lang="zh-CN" class="">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="360-site-verification" content="51b0574ab712ba1d2b812289b762db51">
    <meta name="sogou_site_verification" content="yPNEAdwdAL">
    <title>Guava - 拯救垃圾代码，写出优雅高效，效率提升N倍 | 未读代码 | Java 知识分享</title>
    <link ref="canonical" href="https://www.wdbyte.com/2020/10/develop/google-guava/">
    <link rel="dns-prefetch" href="https://cdn.jsdelivr.net">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css"
          type="text/css">
    <link rel="stylesheet" href="css/index.css">

    <script>if (window.location.hostname !== 'localhost') {
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());
        gtag('config', 'UA-130656610-2');
    }</script>

    <script>var Stun = window.Stun || {};
    var CONFIG = {
        root: '/',
        algolia: undefined,
        fontIcon: {
            "prompt": {
                "success": "fas fa-check-circle",
                "info": "fas fa-arrow-circle-right",
                "warning": "fas fa-exclamation-circle",
                "error": "fas fa-times-circle"
            }, "copyBtn": "fas fa-copy"
        },
        sidebar: {"offsetTop": "20px", "tocMaxDepth": 6},
        header: undefined,
        postWidget: {"endText": true},
        nightMode: {"enable": true},
        back2top: {"enable": true},
        codeblock: {"style": "carbon", "highlight": "light", "wordWrap": false},
        reward: false,
        fancybox: false,
        zoomImage: {"gapAside": "20px"},
        galleryWaterfall: undefined,
        lazyload: false,
        pjax: undefined,
        externalLink: {"icon": {"enable": true, "name": "fas fa-external-link-alt"}},
        shortcuts: undefined,
        prompt: {"copyButton": "复制", "copySuccess": "复制成功", "copyError": "复制失败"},
        sourcePath: {"js": "js", "css": "css", "images": "images"},
    };

    window.CONFIG = CONFIG;</script>
    <meta name="generator" content="Hexo 5.0.2">
</head>
<body style="position: static;" class="">
<div class="container" id="container">
    <header class="header" id="header">
        <div class="header-inner header-inner--height header-inner--bgcolor">
            <nav class="header-nav slider--down slider--clear">
                <div class="header-nav-inner">
                    <div class="header-nav-menubtn"><i class="fas fa-bars"></i></div>
                    <div class="header-nav-menu" style="display: none;">
                        <div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span
                                class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span
                                class="header-nav-menu-item__text">首页</span></a></div>
                        <div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="javascript:;"
                                                             onclick="return false;"><span
                                class="header-nav-menu-item__icon"><i class="fas fa-coffee"></i></span><span
                                class="header-nav-menu-item__text">Java</span></a>
                            <div class="header-nav-submenu hide--force"
                                 style="display: block; opacity: 1; transform: translateY(0px);">
                                <div class="header-nav-submenu-item"><a class="header-nav-submenu-item__link"
                                                                        href="/categories/Java-源码分析/"><span
                                        class="header-nav-submenu-item__text">源码分析</span></a></div>
                                <div class="header-nav-submenu-item"><a class="header-nav-submenu-item__link"
                                                                        href="/categories/Java-%E6%96%B0%E7%89%B9%E6%80%A7/"><span
                                        class="header-nav-submenu-item__text">历史特性</span></a></div>
                            </div>
                        </div>
                        <div class="header-nav-menu-item"><a class="header-nav-menu-item__link"
                                                             href="/categories/Springboot"><span
                                class="header-nav-menu-item__icon"><i class="fas fa-leaf"></i></span><span
                                class="header-nav-menu-item__text">Springboot</span></a></div>
                        <div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span
                                class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span
                                class="header-nav-menu-item__text">归档</span></a></div>
                        <div class="header-nav-menu-item"><a class="header-nav-menu-item__link"
                                                             href="/categories/"><span
                                class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span
                                class="header-nav-menu-item__text">分类</span></a></div>
                    </div>
                    <div class="header-nav-search"><span class="header-nav-search__icon"><i
                            class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div>
                    <div class="header-nav-mode">
                        <div class="mode">
                            <div class="mode-track"><span class="mode-track-moon"></span><span
                                    class="mode-track-sun"></span></div>
                            <div class="mode-thumb"></div>
                        </div>
                    </div>
                </div>
            </nav>
        </div>
    </header>
    <main class="main" id="main">
        <div class="main-inner">
            <div class="content-wrap" id="content-wrap">
                <div class="content" id="content"><!-- Just used to judge whether it is an article page-->
                    <div id="is-post"></div>
                    <div class="post">
                        <header class="post-header"><h1 class="post-title">Guava - 拯救垃圾代码，写出优雅高效，效率提升N倍</h1>
                            <div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span
                                    class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span
                                    class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2020-10-26</span></span><span
                                    class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i
                                    class="far fa-file-word"></i></span><span
                                    class="post-meta-item__info">字数统计</span><span
                                    class="post-meta-item__value">3.9k</span></span><span
                                    class="post-meta-item post-meta-item--readtime"><span
                                    class="post-meta-item__icon"><i class="far fa-clock"></i></span><span
                                    class="post-meta-item__info">阅读时长</span><span
                                    class="post-meta-item__value">19分</span></span></div>
                        </header>
                        <div class="post-body"><p><img
                                src="https://cdn.jsdelivr.net/gh/niumoo/cdn-assets/2020/image-20201022013126998.png"
                                alt="Google Guava" class="zoomimg"></p>
                            <blockquote>
                                <p>最近在看一个同学代码的时候，发现代码中大量使用了 Google 开源的 Guava 核心库中的内容，让代码简单清晰了不少，故学习分享出 Guava
                                    中我认为最实用的功能。</p>
                            </blockquote>
                            <p><strong>Guava</strong> 项目是 Google 公司开源的 Java 核心库，它主要是包含一些在 Java
                                开发中经常使用到的功能，如<strong>数据校验</strong>、<strong>不可变集合</strong>、计数集合，集合增强操作、I/O、缓存、字符串操作等。并且
                                <strong>Guava</strong> 广泛用于 Google 内部的 Java 项目中，也被其他公司广泛使用，甚至在新版 JDK 中直接引入了 <strong>Guava</strong>
                                中的优秀类库，所以质量毋庸置疑。</p>
                            <p>使用方式直接 mavan 依赖引入。</p>
                            <figure class="highlight xml">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line"><span class="comment">&lt;!-- https://mvnrepository.com/artifact/com.google.guava/guava --&gt;</span></span><br><span
                                                        class="line"><span class="tag">&lt;<span
                                                        class="name">dependency</span>&gt;</span></span><br><span
                                                        class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.google.guava<span
                                                        class="tag">&lt;/<span
                                                        class="name">groupId</span>&gt;</span></span><br><span
                                                        class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>guava<span
                                                        class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span
                                                        class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>30.0-jre<span
                                                        class="tag">&lt;/<span
                                                        class="name">version</span>&gt;</span></span><br><span
                                                        class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <a id="more"></a>


                            <h2 id="数据校验">
                                <a href="#数据校验" class="heading-link"><i class="fas fa-link"></i></a>数据校验</h2>
                            <p>数据校验说来十分简单，一是<strong>非空判断</strong>，二是<strong>预期值判断</strong>。非空判断我想每一个 Java
                                开发者都很熟悉，一开始都经常和 <code>NullPointException</code> 打交道。处理的方式我们自然是一个 <code>if( xx ==
                                    null)</code> 就能轻松解决。预期值判断也是类似，检查数据值是不是自己想要的结果即可。</p>
                            <p>即使这么简单的操作，我们是不是还经常出错呢？而且写起来的代码总是一行判断一行异常抛出，怎么看都觉得那么优雅。还好，现在就来尝试第一次使用 Guava 吧。</p>

                            <h3 id="非空判断">
                                <a href="#非空判断" class="heading-link"><i class="fas fa-link"></i></a>非空判断</h3>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">String param = <span
                                                        class="string">"未读代码"</span>;</span><br><span class="line">String name = Preconditions.checkNotNull(param);</span><br><span
                                                        class="line">System.out.println(name); <span class="comment">// 未读代码</span></span><br><span
                                                        class="line">String param2 = <span class="keyword">null</span>;</span><br><span
                                                        class="line">String name2 = Preconditions.checkNotNull(param2); <span
                                                        class="comment">// NullPointerException</span></span><br><span
                                                        class="line">System.out.println(name2);</span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>引入了 Guava 后可以直接使用 <code>Preconditions.checkNotNull</code>
                                进行非空判断，好处为觉得有两个，一是语义清晰代码优雅；二是你也可以自定义报错信息，这样如果参数为空，报错的信息清晰，可以直接定位到具体参数。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">String param2 = <span
                                                        class="keyword">null</span>;</span><br><span class="line">String name2 = Preconditions.checkNotNull(param2,<span
                                                        class="string">"param2 is null"</span>);</span><br><span
                                                        class="line"><span class="comment">// java.lang.NullPointerException: param2 is null</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>


                            <h3 id="预期值判断">
                                <a href="#预期值判断" class="heading-link"><i class="fas fa-link"></i></a>预期值判断</h3>
                            <p>和非空判断类似，可以比较当前值和预期值，如果不相等可以自定义报错信息抛出。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">String param = <span class="string">"www.wdbyte.com2"</span>;</span><br><span
                                                        class="line">String wdbyte = <span class="string">"www.wdbyte.com"</span>;</span><br><span
                                                        class="line">Preconditions.checkArgument(wdbyte.equals(param), <span
                                                        class="string">"[%s] 404 NOT FOUND"</span>, param);</span><br><span
                                                        class="line"><span class="comment">// java.lang.IllegalArgumentException: [www.wdbyte.com2] 404 NOT FOUND</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>


                            <h3 id="是否越界">
                                <a href="#是否越界" class="heading-link"><i class="fas fa-link"></i></a>是否越界</h3>
                            <p><code>Preconditions</code> 类还可以用来检查数组和集合的元素获取是否越界。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line"><span
                                                        class="comment">// Guava 中快速创建ArrayList</span></span><br><span
                                                        class="line">List&lt;String&gt; list = Lists.newArrayList(<span
                                                        class="string">"a"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>, <span
                                                        class="string">"d"</span>);</span><br><span class="line"><span
                                                        class="comment">// 开始校验</span></span><br><span
                                                        class="line"><span class="keyword">int</span> index = Preconditions.checkElementIndex(<span
                                                        class="number">5</span>, list.size());</span><br><span
                                                        class="line"><span class="comment">// java.lang.IndexOutOfBoundsException: index (5) must be less than size (4)</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>代码中快速创建 List 的方式也是 Guava 提供的，后面会详细介绍 Guava 中集合创建的超多姿势。</p>

                            <h2 id="不可变的集合">
                                <a href="#不可变的集合" class="heading-link"><i class="fas fa-link"></i></a>不可变的集合</h2>
                            <p>创建不可变集合是我个人最喜欢 Guava 的一个原因，因为创建一个<strong>不能删除、不能修改、不能增加元素</strong>的集合实在是太实用了。这样的集合你完全不用担心发生什么问题，总的来说有下面几个优点：
                            </p>
                            <ol>
                                <li>线程安全，因为不能修改任何元素，可以随意多线程使用且没有并发问题。</li>
                                <li>可以无忧的提供给第三方使用，反正修改不了。</li>
                                <li>减少内存占用，因为不能改变，所以内部实现可以最大程度节约内存占用。</li>
                                <li>可以用作常量集合。</li>
                            </ol>

                            <h3 id="创建方式">
                                <a href="#创建方式" class="heading-link"><i class="fas fa-link"></i></a>创建方式</h3>
                            <p>说了那么多，那么到底怎么使用呢？赶紧撸起代码来。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br><span class="line">8</span><br><span
                                                        class="line">9</span><br><span class="line">10</span><br><span
                                                        class="line">11</span><br><span class="line">12</span><br><span
                                                        class="line">13</span><br><span class="line">14</span><br><span
                                                        class="line">15</span><br><span class="line">16</span><br><span
                                                        class="line">17</span><br><span class="line">18</span><br><span
                                                        class="line">19</span><br><span class="line">20</span><br><span
                                                        class="line">21</span><br><span class="line">22</span><br><span
                                                        class="line">23</span><br><span class="line">24</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line"><span
                                                        class="comment">// 创建方式1：of</span></span><br><span class="line">ImmutableSet&lt;String&gt; immutableSet = ImmutableSet.of(<span
                                                        class="string">"a"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>);</span><br><span class="line">immutableSet.forEach(System.out::println);</span><br><span
                                                        class="line"><span class="comment">// a</span></span><br><span
                                                        class="line"><span class="comment">// b</span></span><br><span
                                                        class="line"><span class="comment">// c</span></span><br><span
                                                        class="line"></span><br><span class="line"><span
                                                        class="comment">// 创建方式2：builder</span></span><br><span
                                                        class="line">ImmutableSet&lt;String&gt; immutableSet2 = ImmutableSet.&lt;String&gt;builder()</span><br><span
                                                        class="line">    .add(<span
                                                        class="string">"hello"</span>)</span><br><span class="line">    .add(<span
                                                        class="keyword">new</span> String(<span
                                                        class="string">"未读代码"</span>))</span><br><span class="line">    .build();</span><br><span
                                                        class="line">immutableSet2.forEach(System.out::println);</span><br><span
                                                        class="line"><span
                                                        class="comment">// hello</span></span><br><span
                                                        class="line"><span
                                                        class="comment">// 未读代码</span></span><br><span
                                                        class="line"></span><br><span class="line"><span
                                                        class="comment">// 创建方式3：从其他集合中拷贝创建</span></span><br><span
                                                        class="line">ArrayList&lt;String&gt; arrayList = <span
                                                        class="keyword">new</span> ArrayList();</span><br><span
                                                        class="line">arrayList.add(<span
                                                        class="string">"www.wdbyte.com"</span>);</span><br><span
                                                        class="line">arrayList.add(<span class="string">"https"</span>);</span><br><span
                                                        class="line">ImmutableSet&lt;String&gt; immutableSet3 = ImmutableSet.copyOf(arrayList);</span><br><span
                                                        class="line">immutableSet3.forEach(System.out::println);</span><br><span
                                                        class="line"><span
                                                        class="comment">// www.wdbyte.com</span></span><br><span
                                                        class="line"><span
                                                        class="comment">// https</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>都可以正常打印遍历结果，但是如果进行增删改，会直接报 <code>UnsupportedOperationException</code> .</p>
                            <p>其实 JDK 中也提供了一个不可变集合，可以像下面这样创建。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">ArrayList&lt;String&gt; arrayList = <span
                                                        class="keyword">new</span> ArrayList();</span><br><span
                                                        class="line">arrayList.add(<span
                                                        class="string">"www.wdbyte.com"</span>);</span><br><span
                                                        class="line">arrayList.add(<span class="string">"https"</span>);</span><br><span
                                                        class="line"><span
                                                        class="comment">// JDK Collections 创建不可变 List</span></span><br><span
                                                        class="line">List&lt;String&gt; list = Collections.unmodifiableList(arrayList);</span><br><span
                                                        class="line">list.forEach(System.out::println);<span
                                                        class="comment">// www.wdbyte.com https</span></span><br><span
                                                        class="line">list.add(<span class="string">"未读代码"</span>); <span
                                                        class="comment">// java.lang.UnsupportedOperationException</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>


                            <h3 id="注意事项">
                                <a href="#注意事项" class="heading-link"><i class="fas fa-link"></i></a>注意事项</h3>
                            <ol>
                                <li><p>使用 Guava 创建的不可变集合是拒绝 <code>null</code> 值的，因为在 Google 内部调查中，95% 的情况下都不需要放入 <code>null</code>
                                    值。</p>
                                </li>
                                <li><p>使用 JDK 提供的不可变集合创建成功后，原集合添加元素会体现在不可变集合中，而 Guava 的不可变集合不会有这个问题。</p>
                                    <figure class="highlight java">
                                        <figcaption class="custom">
                                            <div class="custom-carbon">
                                                <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                            </div>
                                        </figcaption>
                                        <div class="table-container">
                                            <table>
                                                <tbody>
                                                <tr>
                                                    <td class="gutter">
                                                        <pre><span class="line">1</span><br><span
                                                                class="line">2</span><br><span class="line">3</span><br><span
                                                                class="line">4</span><br><span class="line">5</span><br><span
                                                                class="line">6</span><br><span class="line">7</span><br><span
                                                                class="line">8</span><br><span class="line">9</span><br></pre>
                                                    </td>
                                                    <td class="code">
                                                        <pre><span class="line">List&lt;String&gt; arrayList = <span
                                                                class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span
                                                                class="line">arrayList.add(<span
                                                                class="string">"a"</span>);</span><br><span
                                                                class="line">arrayList.add(<span
                                                                class="string">"b"</span>);</span><br><span
                                                                class="line">List&lt;String&gt; jdkList = Collections.unmodifiableList(arrayList);</span><br><span
                                                                class="line">ImmutableList&lt;String&gt; immutableList = ImmutableList.copyOf(arrayList);</span><br><span
                                                                class="line">arrayList.add(<span
                                                                class="string">"ccc"</span>);</span><br><span
                                                                class="line">jdkList.forEach(System.out::println);<span
                                                                class="comment">// result: a b ccc</span></span><br><span
                                                                class="line">System.out.println(<span class="string">"-------"</span>);</span><br><span
                                                                class="line">immutableList.forEach(System.out::println);<span
                                                                class="comment">// result: a b</span></span><br></pre>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </figure>
                                </li>
                                <li><p>如果不可变集合的元素是引用对象，那么引用对象的属性是可以更改的。</p>
                                </li>
                            </ol>
                            <p><strong>其他不可变集合</strong></p>
                            <p>不可变集合除了上面演示的 <code>set</code> 之外，还有很多不可变集合，下面是 Guava 中不可变集合和其他集合的对应关系。</p>
                            <div class="table-container">
                                <table>
                                    <thead>
                                    <tr>
                                        <th align="left"><strong>可变集合接口</strong></th>
                                        <th align="left">属于JDK还是Guava</th>
                                        <th align="left"><strong>不可变版本</strong></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr>
                                        <td align="left">Collection</td>
                                        <td align="left">JDK</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableCollection.html"><code>ImmutableCollection</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">List</td>
                                        <td align="left">JDK</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableList.html"><code>ImmutableList</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">Set</td>
                                        <td align="left">JDK</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableSet.html"><code>ImmutableSet</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">SortedSet/NavigableSet</td>
                                        <td align="left">JDK</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableSortedSet.html"><code>ImmutableSortedSet</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">Map</td>
                                        <td align="left">JDK</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableMap.html"><code>ImmutableMap</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">SortedMap</td>
                                        <td align="left">JDK</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableSortedMap.html"><code>ImmutableSortedMap</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left"><span class="exturl"><a class="exturl__link" target="_blank"
                                                                                 rel="noopener"
                                                                                 href="http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multiset">Multiset</a><span
                                                class="exturl__icon"><i
                                                class="fas fa-external-link-alt"></i></span></span></td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableMultiset.html"><code>ImmutableMultiset</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">SortedMultiset</td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release12/javadoc/com/google/common/collect/ImmutableSortedMultiset.html"><code>ImmutableSortedMultiset</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left"><span class="exturl"><a class="exturl__link" target="_blank"
                                                                                 rel="noopener"
                                                                                 href="http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap">Multimap</a><span
                                                class="exturl__icon"><i
                                                class="fas fa-external-link-alt"></i></span></span></td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableMultimap.html"><code>ImmutableMultimap</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">ListMultimap</td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableListMultimap.html"><code>ImmutableListMultimap</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">SetMultimap</td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableSetMultimap.html"><code>ImmutableSetMultimap</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left"><span class="exturl"><a class="exturl__link" target="_blank"
                                                                                 rel="noopener"
                                                                                 href="http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap">BiMap</a><span
                                                class="exturl__icon"><i
                                                class="fas fa-external-link-alt"></i></span></span></td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableBiMap.html"><code>ImmutableBiMap</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left"><span class="exturl"><a class="exturl__link" target="_blank"
                                                                                 rel="noopener"
                                                                                 href="http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#ClassToInstanceMap">ClassToInstanceMap</a><span
                                                class="exturl__icon"><i
                                                class="fas fa-external-link-alt"></i></span></span></td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableClassToInstanceMap.html"><code>ImmutableClassToInstanceMap</code></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left"><span class="exturl"><a class="exturl__link" target="_blank"
                                                                                 rel="noopener"
                                                                                 href="http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Table">Table</a><span
                                                class="exturl__icon"><i
                                                class="fas fa-external-link-alt"></i></span></span></td>
                                        <td align="left">Guava</td>
                                        <td align="left"><a target="_blank" rel="noopener"
                                                            href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableTable.html"><code>ImmutableTable</code></a>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>

                            <h2 id="集合操作工厂">
                                <a href="#集合操作工厂" class="heading-link"><i class="fas fa-link"></i></a>集合操作工厂</h2>
                            <p> 其实这里只会介绍一个创建方法，但是为什么还是单独拿出来介绍了呢？看下去你就会<strong>大呼好用</strong>。虽然 JDK
                                中已经提供了大量的集合相关的操作方法，用起来也是非常的方便，但是 Guava 还是增加了一些十分好用的方法，保证让你用上一次就爱不释手，</p>

                            <h3 id="创建集合。">
                                <a href="#创建集合。" class="heading-link"><i class="fas fa-link"></i></a>创建集合。</h3>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br><span class="line">8</span><br><span
                                                        class="line">9</span><br><span class="line">10</span><br><span
                                                        class="line">11</span><br><span class="line">12</span><br><span
                                                        class="line">13</span><br><span class="line">14</span><br><span
                                                        class="line">15</span><br><span class="line">16</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line"><span
                                                        class="comment">// 创建一个 ArrayList 集合</span></span><br><span
                                                        class="line">List&lt;String&gt; list1 = Lists.newArrayList();</span><br><span
                                                        class="line"><span
                                                        class="comment">// 创建一个 ArrayList 集合，同时塞入3个数据</span></span><br><span
                                                        class="line">List&lt;String&gt; list2 = Lists.newArrayList(<span
                                                        class="string">"a"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>);</span><br><span class="line"><span
                                                        class="comment">// 创建一个 ArrayList 集合，容量初始化为10</span></span><br><span
                                                        class="line">List&lt;String&gt; list3 = Lists.newArrayListWithCapacity(<span
                                                        class="number">10</span>);</span><br><span
                                                        class="line"></span><br><span class="line">LinkedList&lt;String&gt; linkedList1 = Lists.newLinkedList();</span><br><span
                                                        class="line">CopyOnWriteArrayList&lt;String&gt; cowArrayList = Lists.newCopyOnWriteArrayList();</span><br><span
                                                        class="line"></span><br><span class="line">HashMap&lt;Object, Object&gt; hashMap = Maps.newHashMap();</span><br><span
                                                        class="line">ConcurrentMap&lt;Object, Object&gt; concurrentMap = Maps.newConcurrentMap();</span><br><span
                                                        class="line">TreeMap&lt;Comparable, Object&gt; treeMap = Maps.newTreeMap();</span><br><span
                                                        class="line"></span><br><span class="line">HashSet&lt;Object&gt; hashSet = Sets.newHashSet();</span><br><span
                                                        class="line">HashSet&lt;String&gt; newHashSet = Sets.newHashSet(<span
                                                        class="string">"a"</span>, <span
                                                        class="string">"a"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>);</span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>Guava 为每一个集合都添加了工厂方法创建方式，上面已经展示了部分集合的工厂方法创建方式。是不是十分的好用呢。而且可以在创建时直接扔进去几个元素，这个简直太赞了，再也不用一个个
                                <code>add</code> 了。</p>

                            <h3 id="集合交集并集差集">
                                <a href="#集合交集并集差集" class="heading-link"><i class="fas fa-link"></i></a>集合交集并集差集</h3>
                            <p>过于简单，直接看代码和输出结果吧。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br><span class="line">8</span><br><span
                                                        class="line">9</span><br><span class="line">10</span><br><span
                                                        class="line">11</span><br><span class="line">12</span><br><span
                                                        class="line">13</span><br><span class="line">14</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">Set&lt;String&gt; newHashSet1 = Sets.newHashSet(<span
                                                        class="string">"a"</span>, <span
                                                        class="string">"a"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>);</span><br><span class="line">Set&lt;String&gt; newHashSet2 = Sets.newHashSet(<span
                                                        class="string">"b"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>, <span
                                                        class="string">"d"</span>);</span><br><span class="line"></span><br><span
                                                        class="line"><span class="comment">// 交集</span></span><br><span
                                                        class="line">SetView&lt;String&gt; intersectionSet = Sets.intersection(newHashSet1, newHashSet2);</span><br><span
                                                        class="line">System.out.println(intersectionSet); <span
                                                        class="comment">// [b, c]</span></span><br><span
                                                        class="line"></span><br><span class="line"><span
                                                        class="comment">// 并集</span></span><br><span class="line">SetView&lt;String&gt; unionSet = Sets.union(newHashSet1, newHashSet2);</span><br><span
                                                        class="line">System.out.println(unionSet); <span
                                                        class="comment">// [a, b, c, d]</span></span><br><span
                                                        class="line"></span><br><span class="line"><span
                                                        class="comment">// newHashSet1 中存在，newHashSet2 中不存在</span></span><br><span
                                                        class="line">SetView&lt;String&gt; setView = Sets.difference(newHashSet1, newHashSet2);</span><br><span
                                                        class="line">System.out.println(setView); <span class="comment">// [a]</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>


                            <h2 id="有数量的集合">
                                <a href="#有数量的集合" class="heading-link"><i class="fas fa-link"></i></a>有数量的集合</h2>
                            <p>这个真的太有用了，因为我们经常会需要设计可以计数的集合，或者 value 是 <code>List</code> 的 <code>Map</code>
                                集合，如果说你不太明白，看下面这段代码，是否某天夜里你也这样写过。</p>
                            <ol>
                                <li><p>统计相同元素出现的次数（下面的代码我已经尽可能精简写法了）。</p>
                                    <p>JDK 原生写法：</p>
                                    <figure class="highlight java">
                                        <figcaption class="custom">
                                            <div class="custom-carbon">
                                                <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                            </div>
                                        </figcaption>
                                        <div class="table-container">
                                            <table>
                                                <tbody>
                                                <tr>
                                                    <td class="gutter">
                                                        <pre><span class="line">1</span><br><span
                                                                class="line">2</span><br><span class="line">3</span><br><span
                                                                class="line">4</span><br><span class="line">5</span><br><span
                                                                class="line">6</span><br><span class="line">7</span><br><span
                                                                class="line">8</span><br><span class="line">9</span><br><span
                                                                class="line">10</span><br><span
                                                                class="line">11</span><br><span
                                                                class="line">12</span><br><span
                                                                class="line">13</span><br><span
                                                                class="line">14</span><br><span
                                                                class="line">15</span><br><span
                                                                class="line">16</span><br></pre>
                                                    </td>
                                                    <td class="code">
                                                        <pre><span class="line"><span class="comment">// Java 统计相同元素出现的次数。</span></span><br><span
                                                                class="line">List&lt;String&gt; words = Lists.newArrayList(<span
                                                                class="string">"a"</span>, <span
                                                                class="string">"b"</span>, <span
                                                                class="string">"c"</span>, <span
                                                                class="string">"d"</span>, <span
                                                                class="string">"a"</span>, <span
                                                                class="string">"c"</span>);</span><br><span
                                                                class="line">Map&lt;String, Integer&gt; countMap = <span
                                                                class="keyword">new</span> HashMap&lt;String, Integer&gt;();</span><br><span
                                                                class="line"><span class="keyword">for</span> (String word : words) {</span><br><span
                                                                class="line">    Integer count = countMap.get(word);</span><br><span
                                                                class="line">    count = (count == <span
                                                                class="keyword">null</span>) ? <span
                                                                class="number">1</span> : ++count;</span><br><span
                                                                class="line">    countMap.put(word, count);</span><br><span
                                                                class="line">}</span><br><span class="line">countMap.forEach((k, v) -&gt; System.out.println(k + <span
                                                                class="string">":"</span> + v));</span><br><span
                                                                class="line"><span class="comment">/**</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * result:</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * a:2</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * b:1</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * c:2</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * d:1</span></span><br><span
                                                                class="line"><span class="comment"> */</span></span><br></pre>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </figure>

                                    <p>尽管已经尽量优化代码，代码量还是不少的，那么在 Guava 中有什么不一样呢？在 Guava. 中主要是使用 <code>HashMultiset</code>
                                        类，看下面。</p>
                                    <figure class="highlight java">
                                        <figcaption class="custom">
                                            <div class="custom-carbon">
                                                <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                            </div>
                                        </figcaption>
                                        <div class="table-container">
                                            <table>
                                                <tbody>
                                                <tr>
                                                    <td class="gutter">
                                                        <pre><span class="line">1</span><br><span
                                                                class="line">2</span><br><span class="line">3</span><br><span
                                                                class="line">4</span><br><span class="line">5</span><br><span
                                                                class="line">6</span><br><span class="line">7</span><br><span
                                                                class="line">8</span><br><span class="line">9</span><br><span
                                                                class="line">10</span><br></pre>
                                                    </td>
                                                    <td class="code">
                                                        <pre><span class="line">ArrayList&lt;String&gt; arrayList = Lists.newArrayList(<span
                                                                class="string">"a"</span>, <span
                                                                class="string">"b"</span>, <span
                                                                class="string">"c"</span>, <span
                                                                class="string">"d"</span>, <span
                                                                class="string">"a"</span>, <span
                                                                class="string">"c"</span>);</span><br><span
                                                                class="line">HashMultiset&lt;String&gt; multiset = HashMultiset.create(arrayList);</span><br><span
                                                                class="line">multiset.elementSet().forEach(s -&gt; System.out.println(s + <span
                                                                class="string">":"</span> + multiset.count(s)));</span><br><span
                                                                class="line"><span class="comment">/**</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * result:</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * a:2</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * b:1</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * c:2</span></span><br><span
                                                                class="line"><span
                                                                class="comment"> * d:1</span></span><br><span
                                                                class="line"><span class="comment"> */</span></span><br></pre>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </figure>

                                    <p>是的，只要把元素添加进去就行了，不用在乎是否重复，最后都可以使用 <code>count</code> 方法统计重复元素数量。看着舒服，写着优雅，<code>HashMultiset</code>
                                        是 Guava 中实现的 <code>Collection</code> 类，可以轻松统计元素数量。</p>
                                </li>
                                <li><p>一对多，value 是 <code>List</code> 的 <code>Map</code> 集合。</p>
                                    <p>假设一个场景，需要把很多动物按照种类进行分类，我相信最后你会写出类似的代码。</p>
                                    <p>JDK 原生写法：</p>
                                    <figure class="highlight java">
                                        <figcaption class="custom">
                                            <div class="custom-carbon">
                                                <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                            </div>
                                        </figcaption>
                                        <div class="table-container">
                                            <table>
                                                <tbody>
                                                <tr>
                                                    <td class="gutter">
                                                        <pre><span class="line">1</span><br><span
                                                                class="line">2</span><br><span class="line">3</span><br><span
                                                                class="line">4</span><br><span class="line">5</span><br><span
                                                                class="line">6</span><br><span class="line">7</span><br><span
                                                                class="line">8</span><br><span class="line">9</span><br><span
                                                                class="line">10</span><br></pre>
                                                    </td>
                                                    <td class="code">
                                                        <pre><span class="line">HashMap&lt;String, Set&lt;String&gt;&gt; animalMap = <span
                                                                class="keyword">new</span> HashMap&lt;&gt;();</span><br><span
                                                                class="line">HashSet&lt;String&gt; dogSet = <span
                                                                class="keyword">new</span> HashSet&lt;&gt;();</span><br><span
                                                                class="line">dogSet.add(<span class="string">"旺财"</span>);</span><br><span
                                                                class="line">dogSet.add(<span class="string">"大黄"</span>);</span><br><span
                                                                class="line">animalMap.put(<span
                                                                class="string">"狗"</span>, dogSet);</span><br><span
                                                                class="line">HashSet&lt;String&gt; catSet = <span
                                                                class="keyword">new</span> HashSet&lt;&gt;();</span><br><span
                                                                class="line">catSet.add(<span class="string">"加菲"</span>);</span><br><span
                                                                class="line">catSet.add(<span class="string">"汤姆"</span>);</span><br><span
                                                                class="line">animalMap.put(<span
                                                                class="string">"猫"</span>, catSet);</span><br><span
                                                                class="line">System.out.println(animalMap.get(<span
                                                                class="string">"猫"</span>)); <span class="comment">// [加菲, 汤姆]</span></span><br></pre>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </figure>

                                    <p>最后一行查询猫得到了猫类的 “加菲” 和 ”汤姆“。这个代码简直太烦做了，如果使用 Guava 呢？</p>
                                    <figure class="highlight java">
                                        <figcaption class="custom">
                                            <div class="custom-carbon">
                                                <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                                <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                            </div>
                                        </figcaption>
                                        <div class="table-container">
                                            <table>
                                                <tbody>
                                                <tr>
                                                    <td class="gutter">
                                                        <pre><span class="line">1</span><br><span
                                                                class="line">2</span><br><span class="line">3</span><br><span
                                                                class="line">4</span><br><span class="line">5</span><br><span
                                                                class="line">6</span><br><span class="line">7</span><br></pre>
                                                    </td>
                                                    <td class="code">
                                                        <pre><span class="line"><span
                                                                class="comment">// use guava</span></span><br><span
                                                                class="line">HashMultimap&lt;String, String&gt; multimap = HashMultimap.create();</span><br><span
                                                                class="line">multimap.put(<span
                                                                class="string">"狗"</span>, <span
                                                                class="string">"大黄"</span>);</span><br><span
                                                                class="line">multimap.put(<span
                                                                class="string">"狗"</span>, <span
                                                                class="string">"旺财"</span>);</span><br><span
                                                                class="line">multimap.put(<span
                                                                class="string">"猫"</span>, <span
                                                                class="string">"加菲"</span>);</span><br><span
                                                                class="line">multimap.put(<span
                                                                class="string">"猫"</span>, <span
                                                                class="string">"汤姆"</span>);</span><br><span
                                                                class="line">System.out.println(multimap.get(<span
                                                                class="string">"猫"</span>)); <span class="comment">// [加菲, 汤姆]</span></span><br></pre>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </figure>

                                    <p>HashMultimap 可以扔进去重复的 key 值，最后获取时可以得到所有的 value 值，可以看到输出结果和 JDK
                                        写法上是一样的，但是代码已经无比清爽。</p>
                                </li>
                            </ol>

                            <h2 id="字符串操作">
                                <a href="#字符串操作" class="heading-link"><i class="fas fa-link"></i></a>字符串操作</h2>
                            <p>作为开发中最长使用的数据类型，字符串操作的增强可以让开发更加高效。</p>

                            <h3 id="字符拼接">
                                <a href="#字符拼接" class="heading-link"><i class="fas fa-link"></i></a>字符拼接</h3>
                            <p>JDK 8 中其实已经内置了字符串拼接方法，但是它只是简单的拼接，没有额外操作，比如过滤掉 null 元素，去除前后空格等。先看一下 JDK 8 中字符串拼接的几种方式。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br><span class="line">8</span><br><span
                                                        class="line">9</span><br><span class="line">10</span><br><span
                                                        class="line">11</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line"><span
                                                        class="comment">// JDK 方式一</span></span><br><span class="line">ArrayList&lt;String&gt; list = Lists.newArrayList(<span
                                                        class="string">"a"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>, <span
                                                        class="keyword">null</span>);</span><br><span class="line">String join = String.join(<span
                                                        class="string">","</span>, list);</span><br><span class="line">System.out.println(join); <span
                                                        class="comment">// a,b,c,null</span></span><br><span
                                                        class="line"><span
                                                        class="comment">// JDK 方式二</span></span><br><span class="line">String result = list.stream().collect(Collectors.joining(<span
                                                        class="string">","</span>));</span><br><span class="line">System.out.println(result); <span
                                                        class="comment">// a,b,c,null</span></span><br><span
                                                        class="line"><span
                                                        class="comment">// JDK 方式三</span></span><br><span class="line">StringJoiner stringJoiner = <span
                                                        class="keyword">new</span> StringJoiner(<span
                                                        class="string">","</span>);</span><br><span class="line">list.forEach(stringJoiner::add);</span><br><span
                                                        class="line">System.out.println(stringJoiner.toString()); <span
                                                        class="comment">// a,b,c,null</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>可以看到 null 值也被拼接到了字符串里，这有时候不是我们想要的，那么使用 Guava 有什么不一样呢？</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">ArrayList&lt;String&gt; list = Lists.newArrayList(<span
                                                        class="string">"a"</span>, <span
                                                        class="string">"b"</span>, <span
                                                        class="string">"c"</span>, <span
                                                        class="keyword">null</span>);</span><br><span class="line">String join = Joiner.on(<span
                                                        class="string">","</span>).skipNulls().join(list);</span><br><span
                                                        class="line">System.out.println(join); <span class="comment">// a,b,c</span></span><br><span
                                                        class="line"></span><br><span class="line">String join1 = Joiner.on(<span
                                                        class="string">","</span>).useForNull(<span
                                                        class="string">"空值"</span>).join(<span
                                                        class="string">"旺财"</span>, <span
                                                        class="string">"汤姆"</span>, <span
                                                        class="string">"杰瑞"</span>, <span class="keyword">null</span>);</span><br><span
                                                        class="line">System.out.println(join1); <span class="comment">// 旺财,汤姆,杰瑞,空值</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>可以看到使用 <code>skipNulls()</code> 可以跳过空值，使用 <code>useFornull(String)</code> 可以为空值自定义显示文本。
                            </p>

                            <h3 id="字符串分割">
                                <a href="#字符串分割" class="heading-link"><i class="fas fa-link"></i></a>字符串分割</h3>
                            <p>JDK 中是自带字符串分割的，我想你也一定用过，那就是 String 的 split
                                方法，但是这个方法有一个问题，就是如果最后一个元素为空，那么就会丢弃，奇怪的是第一个元素为空却不会丢弃，这就十分迷惑，下面通过一个例子演示这个问题。</p>
                            <figure class="highlight plain">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br><span class="line">8</span><br><span
                                                        class="line">9</span><br><span class="line">10</span><br><span
                                                        class="line">11</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">String str = ",a,,b,";</span><br><span
                                                        class="line">String[] splitArr = str.split(",");</span><br><span
                                                        class="line">Arrays.stream(splitArr).forEach(System.out::println);</span><br><span
                                                        class="line">System.out.println("------");</span><br><span
                                                        class="line">/**</span><br><span class="line"> *</span><br><span
                                                        class="line"> * a</span><br><span
                                                        class="line"> * </span><br><span
                                                        class="line"> * b</span><br><span
                                                        class="line"> * ------</span><br><span
                                                        class="line"> */</span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>你也可以自己测试下，最后一个元素不是空，直接消失了。</p>
                            <p>如果使用 Guava 是怎样的操作方式呢？Guava 提供了 Splitter 类，并且有一系列的操作方式可以直观的控制分割逻辑。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br><span class="line">8</span><br><span
                                                        class="line">9</span><br><span class="line">10</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line">String str = <span
                                                        class="string">",a ,,b ,"</span>;</span><br><span class="line">Iterable&lt;String&gt; split = Splitter.on(<span
                                                        class="string">","</span>)</span><br><span class="line">    .omitEmptyStrings() <span
                                                        class="comment">// 忽略空值</span></span><br><span class="line">    .trimResults() <span
                                                        class="comment">// 过滤结果中的空白</span></span><br><span class="line">    .split(str);</span><br><span
                                                        class="line">split.forEach(System.out::println);</span><br><span
                                                        class="line"><span class="comment">/**</span></span><br><span
                                                        class="line"><span class="comment"> * a</span></span><br><span
                                                        class="line"><span class="comment"> * b</span></span><br><span
                                                        class="line"><span class="comment"> */</span></span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>


                            <h2 id="缓存">
                                <a href="#缓存" class="heading-link"><i class="fas fa-link"></i></a>缓存</h2>
                            <p>在开发中我们可能需要使用小规模的缓存，来提高访问速度。这时引入专业的缓存中间件可能又觉得浪费。现在可以了， Guava
                                中提供了简单的缓存类，且可以根据预计容量、过期时间等自动过期已经添加的元素。即使这样我们也要预估好可能占用的内存空间，以防内存占用过多。</p>
                            <p>现在看一下在 Guava 中缓存该怎么用。</p>
                            <figure class="highlight java">
                                <figcaption class="custom">
                                    <div class="custom-carbon">
                                        <div class="custom-carbon-dot custom-carbon-dot--red"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--yellow"></div>
                                        <div class="custom-carbon-dot custom-carbon-dot--green"></div>
                                    </div>
                                </figcaption>
                                <div class="table-container">
                                    <table>
                                        <tbody>
                                        <tr>
                                            <td class="gutter">
                                                <pre><span class="line">1</span><br><span class="line">2</span><br><span
                                                        class="line">3</span><br><span class="line">4</span><br><span
                                                        class="line">5</span><br><span class="line">6</span><br><span
                                                        class="line">7</span><br><span class="line">8</span><br><span
                                                        class="line">9</span><br><span class="line">10</span><br><span
                                                        class="line">11</span><br><span class="line">12</span><br><span
                                                        class="line">13</span><br><span class="line">14</span><br><span
                                                        class="line">15</span><br><span class="line">16</span><br><span
                                                        class="line">17</span><br><span class="line">18</span><br><span
                                                        class="line">19</span><br><span class="line">20</span><br><span
                                                        class="line">21</span><br><span class="line">22</span><br><span
                                                        class="line">23</span><br><span class="line">24</span><br><span
                                                        class="line">25</span><br><span class="line">26</span><br><span
                                                        class="line">27</span><br><span class="line">28</span><br><span
                                                        class="line">29</span><br><span class="line">30</span><br><span
                                                        class="line">31</span><br><span class="line">32</span><br><span
                                                        class="line">33</span><br><span class="line">34</span><br><span
                                                        class="line">35</span><br><span class="line">36</span><br><span
                                                        class="line">37</span><br><span class="line">38</span><br><span
                                                        class="line">39</span><br><span class="line">40</span><br><span
                                                        class="line">41</span><br><span class="line">42</span><br><span
                                                        class="line">43</span><br><span class="line">44</span><br><span
                                                        class="line">45</span><br><span class="line">46</span><br><span
                                                        class="line">47</span><br><span class="line">48</span><br><span
                                                        class="line">49</span><br><span class="line">50</span><br><span
                                                        class="line">51</span><br><span class="line">52</span><br><span
                                                        class="line">53</span><br><span class="line">54</span><br><span
                                                        class="line">55</span><br><span class="line">56</span><br><span
                                                        class="line">57</span><br><span class="line">58</span><br><span
                                                        class="line">59</span><br><span class="line">60</span><br><span
                                                        class="line">61</span><br><span class="line">62</span><br><span
                                                        class="line">63</span><br><span class="line">64</span><br><span
                                                        class="line">65</span><br></pre>
                                            </td>
                                            <td class="code">
                                                <pre><span class="line"><span class="meta">@Test</span></span><br><span
                                                        class="line"><span class="function"><span
                                                        class="keyword">public</span> <span class="keyword">void</span> <span
                                                        class="title">testCache</span><span
                                                        class="params">()</span> <span class="keyword">throws</span> ExecutionException, InterruptedException </span>{</span><br><span
                                                        class="line"></span><br><span class="line">    CacheLoader cacheLoader = <span
                                                        class="keyword">new</span> CacheLoader&lt;String, Animal&gt;() {</span><br><span
                                                        class="line">        <span
                                                        class="comment">// 如果找不到元素，会调用这里</span></span><br><span
                                                        class="line">        <span
                                                        class="meta">@Override</span></span><br><span class="line">        <span
                                                        class="function"><span
                                                        class="keyword">public</span> Animal <span
                                                        class="title">load</span><span class="params">(String s)</span> </span>{</span><br><span
                                                        class="line">            <span
                                                        class="keyword">return</span> <span class="keyword">null</span>;</span><br><span
                                                        class="line">        }</span><br><span
                                                        class="line">    };</span><br><span class="line">    LoadingCache&lt;String, Animal&gt; loadingCache = CacheBuilder.newBuilder()</span><br><span
                                                        class="line">        .maximumSize(<span
                                                        class="number">1000</span>) <span
                                                        class="comment">// 容量</span></span><br><span class="line">        .expireAfterWrite(<span
                                                        class="number">3</span>, TimeUnit.SECONDS) <span
                                                        class="comment">// 过期时间</span></span><br><span class="line">        .removalListener(<span
                                                        class="keyword">new</span> MyRemovalListener()) <span
                                                        class="comment">// 失效监听器</span></span><br><span class="line">        .build(cacheLoader); <span
                                                        class="comment">//</span></span><br><span class="line">    loadingCache.put(<span
                                                        class="string">"狗"</span>, <span class="keyword">new</span> Animal(<span
                                                        class="string">"旺财"</span>, <span
                                                        class="number">1</span>));</span><br><span class="line">    loadingCache.put(<span
                                                        class="string">"猫"</span>, <span class="keyword">new</span> Animal(<span
                                                        class="string">"汤姆"</span>, <span
                                                        class="number">3</span>));</span><br><span class="line">    loadingCache.put(<span
                                                        class="string">"狼"</span>, <span class="keyword">new</span> Animal(<span
                                                        class="string">"灰太狼"</span>, <span
                                                        class="number">4</span>));</span><br><span
                                                        class="line"></span><br><span class="line">    loadingCache.invalidate(<span
                                                        class="string">"猫"</span>); <span class="comment">// 手动失效</span></span><br><span
                                                        class="line"></span><br><span class="line">    Animal animal = loadingCache.get(<span
                                                        class="string">"狼"</span>);</span><br><span class="line">    System.out.println(animal);</span><br><span
                                                        class="line">    Thread.sleep(<span
                                                        class="number">4</span> * <span
                                                        class="number">1000</span>);</span><br><span
                                                        class="line">    <span
                                                        class="comment">// 狼已经自动过去，获取为 null 值报错</span></span><br><span
                                                        class="line">    System.out.println(loadingCache.get(<span
                                                        class="string">"狼"</span>));</span><br><span
                                                        class="line">    <span
                                                        class="comment">/**</span></span><br><span class="line"><span
                                                        class="comment">     * key=猫,value=Animal{name='汤姆', age=3},reason=EXPLICIT</span></span><br><span
                                                        class="line"><span class="comment">     * Animal{name='灰太狼', age=4}</span></span><br><span
                                                        class="line"><span class="comment">     * key=狗,value=Animal{name='旺财', age=1},reason=EXPIRED</span></span><br><span
                                                        class="line"><span class="comment">     * key=狼,value=Animal{name='灰太狼', age=4},reason=EXPIRED</span></span><br><span
                                                        class="line"><span class="comment">     *</span></span><br><span
                                                        class="line"><span class="comment">     * com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key 狼.</span></span><br><span
                                                        class="line"><span
                                                        class="comment">     */</span></span><br><span
                                                        class="line">}</span><br><span class="line"></span><br><span
                                                        class="line"><span class="comment">/**</span></span><br><span
                                                        class="line"><span
                                                        class="comment"> * 缓存移除监听器</span></span><br><span
                                                        class="line"><span class="comment"> */</span></span><br><span
                                                        class="line"><span class="class"><span
                                                        class="keyword">class</span> <span class="title">MyRemovalListener</span> <span
                                                        class="keyword">implements</span> <span class="title">RemovalListener</span>&lt;<span
                                                        class="title">String</span>, <span class="title">Animal</span>&gt; </span>{</span><br><span
                                                        class="line"></span><br><span class="line">    <span
                                                        class="meta">@Override</span></span><br><span
                                                        class="line">    <span class="function"><span class="keyword">public</span> <span
                                                        class="keyword">void</span> <span class="title">onRemoval</span><span
                                                        class="params">(RemovalNotification&lt;String, Animal&gt; notification)</span> </span>{</span><br><span
                                                        class="line">        String reason = String.format(<span
                                                        class="string">"key=%s,value=%s,reason=%s"</span>, notification.getKey(), notification.getValue(), notification.getCause());</span><br><span
                                                        class="line">        System.out.println(reason);</span><br><span
                                                        class="line">    }</span><br><span
                                                        class="line">}</span><br><span class="line"></span><br><span
                                                        class="line"><span class="class"><span
                                                        class="keyword">class</span> <span class="title">Animal</span> </span>{</span><br><span
                                                        class="line">    <span class="keyword">private</span> String name;</span><br><span
                                                        class="line">    <span class="keyword">private</span> Integer age;</span><br><span
                                                        class="line"></span><br><span class="line">    <span
                                                        class="meta">@Override</span></span><br><span
                                                        class="line">    <span class="function"><span class="keyword">public</span> String <span
                                                        class="title">toString</span><span
                                                        class="params">()</span> </span>{</span><br><span class="line">        <span
                                                        class="keyword">return</span> <span
                                                        class="string">"Animal{"</span> +</span><br><span class="line">            <span
                                                        class="string">"name='"</span> + name + <span class="string">'\''</span> +</span><br><span
                                                        class="line">            <span class="string">", age="</span> + age +</span><br><span
                                                        class="line">            <span class="string">'}'</span>;</span><br><span
                                                        class="line">    }</span><br><span class="line"></span><br><span
                                                        class="line">    <span class="function"><span class="keyword">public</span> <span
                                                        class="title">Animal</span><span class="params">(String name, Integer age)</span> </span>{</span><br><span
                                                        class="line">        <span class="keyword">this</span>.name = name;</span><br><span
                                                        class="line">        <span class="keyword">this</span>.age = age;</span><br><span
                                                        class="line">    }</span><br><span
                                                        class="line">}</span><br></pre>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </figure>

                            <p>这个例子中主要分为 CacheLoader、MyRemovalListener、LoadingCache。</p>
                            <p>CacheLoader 中重写了 <code>load</code> 方法，这个方法会在查询缓存没有命中时被调用，我这里直接返回了 <code>null</code>，其实这样会在没有命中时抛出
                                <code>CacheLoader returned null for key </code> 异常信息。</p>
                            <p>MyRemovalListener 作为缓存元素失效时的监听类，在有元素缓存失效时会自动调用 <code>onRemoval</code>
                                方法，这里需要注意的是这个方法是同步方法，如果这里耗时较长，会阻塞直到处理完成。</p>
                            <p>LoadingCache 就是缓存的主要操作对象了，常用的就是其中的 <code>put</code> 和 <code>get</code> 方法了。</p>

                            <h2 id="总结">
                                <a href="#总结" class="heading-link"><i class="fas fa-link"></i></a>总结</h2>
                            <p>上面介绍了我认为最常用的 Guava 功能，Guava 作为 Google 公司开源的 Java
                                开发核心库，个人觉得实用性还是很高的。引入后不仅能快速的实现一些开发中常用的功能，而且还可以让代码更加的优雅简洁。我觉得适用于每一个 Java 项目。Guava
                                的其他的功能你也可以自己去发现。它的 Github 地址是：<span class="exturl"><a class="exturl__link"
                                                                                      target="_blank" rel="noopener"
                                                                                      href="https://github.com/google/guava">https://github.com/google/guava</a><span
                                        class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span>.
                            </p>
                            <p><strong>参考</strong></p>
                            <ol>
                                <li><span class="exturl"><a class="exturl__link" target="_blank" rel="noopener"
                                                            href="https://github.com/google/guava/wiki">https://github.com/google/guava/wiki</a><span
                                        class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></li>
                            </ol>

                            <h2 id="订阅">
                                <a href="#订阅" class="heading-link"><i class="fas fa-link"></i></a>订阅</h2>
                            <p>文章每周持续更新，有帮助可以点个「<strong>赞</strong>」或「<strong>分享</strong>」，都是支持，我都喜欢！ </p>
                            <p>要实时关注更新的文章以及分享的干货，可以关注 <span class="exturl"><a class="exturl__link" target="_blank"
                                                                              rel="noopener"
                                                                              href="https://weixin.sogou.com/weixin?query=%E6%9C%AA%E8%AF%BB%E4%BB%A3%E7%A0%81">未读代码</a><span
                                    class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span>
                                公众号（下方二维码）或者<a href="https://www.wdbyte.com/">我的网站</a>。</p>
                            <p>
                                <img src="https://cdn.jsdelivr.net/gh/niumoo/cdn-assets@439f6a5f6bd130e2aec56f3527656d6edb487b91/webinfo/weixin-public.jpg"
                                     alt="公众号" class="zoomimg"></p>
                        </div>
                        <footer class="post-footer">
                            <div class="post-ending ending">
                                <div class="ending__text">------ 本文结束，感谢您的阅读 ------</div>
                            </div>
                            <div class="post-copyright copyright">
                                <div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span
                                        class="copyright-author__value"><a href="https://www.wdbyte.com">未读代码</a></span>
                                </div>
                                <div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span
                                        class="copyright-link__value"><a
                                        href="https://www.wdbyte.com/2020/10/develop/google-guava/">https://www.wdbyte.com/2020/10/develop/google-guava/</a></span>
                                </div>
                                <div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span
                                        class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a
                                        href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en"
                                        rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div>
                                <div class="copy-button" data-popover="复制" data-popover-pos="up"><i
                                        class="fas fa-copy"></i></div>
                            </div>
                            <div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i
                                    class="fas fa-tag"></i></span><a class="post-tags-item__link"
                                                                     href="https://www.wdbyte.com/tags/Guava/">Guava</a></span>
                            </div>
                        </footer>
                    </div>
                </div>
            </div>
            <div class="sidebar-wrap" id="sidebar-wrap">
                <aside class="sidebar" id="sidebar">
                    <div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span></div>
                    <section class="sidebar-toc" style="display: block; opacity: 1;">
                        <ol class="toc">
                            <li class="toc-item toc-level-2"><a class="toc-link"
                                                                href="#%E6%95%B0%E6%8D%AE%E6%A0%A1%E9%AA%8C"><span
                                    class="toc-text">
          数据校验</span></a>
                                <ol class="toc-child">
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E9%9D%9E%E7%A9%BA%E5%88%A4%E6%96%AD"><span
                                            class="toc-text">
          非空判断</span></a></li>
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E9%A2%84%E6%9C%9F%E5%80%BC%E5%88%A4%E6%96%AD"><span
                                            class="toc-text">
          预期值判断</span></a></li>
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E6%98%AF%E5%90%A6%E8%B6%8A%E7%95%8C"><span
                                            class="toc-text">
          是否越界</span></a></li>
                                </ol>
                            </li>
                            <li class="toc-item toc-level-2"><a class="toc-link"
                                                                href="#%E4%B8%8D%E5%8F%AF%E5%8F%98%E7%9A%84%E9%9B%86%E5%90%88"><span
                                    class="toc-text">
          不可变的集合</span></a>
                                <ol class="toc-child">
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E5%88%9B%E5%BB%BA%E6%96%B9%E5%BC%8F"><span
                                            class="toc-text">
          创建方式</span></a></li>
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span
                                            class="toc-text">
          注意事项</span></a></li>
                                </ol>
                            </li>
                            <li class="toc-item toc-level-2"><a class="toc-link"
                                                                href="#%E9%9B%86%E5%90%88%E6%93%8D%E4%BD%9C%E5%B7%A5%E5%8E%82"><span
                                    class="toc-text">
          集合操作工厂</span></a>
                                <ol class="toc-child">
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E5%88%9B%E5%BB%BA%E9%9B%86%E5%90%88%E3%80%82"><span
                                            class="toc-text">
          创建集合。</span></a></li>
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E9%9B%86%E5%90%88%E4%BA%A4%E9%9B%86%E5%B9%B6%E9%9B%86%E5%B7%AE%E9%9B%86"><span
                                            class="toc-text">
          集合交集并集差集</span></a></li>
                                </ol>
                            </li>
                            <li class="toc-item toc-level-2"><a class="toc-link"
                                                                href="#%E6%9C%89%E6%95%B0%E9%87%8F%E7%9A%84%E9%9B%86%E5%90%88"><span
                                    class="toc-text">
          有数量的集合</span></a></li>
                            <li class="toc-item toc-level-2"><a class="toc-link"
                                                                href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%93%8D%E4%BD%9C"><span
                                    class="toc-text">
          字符串操作</span></a>
                                <ol class="toc-child">
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E5%AD%97%E7%AC%A6%E6%8B%BC%E6%8E%A5"><span
                                            class="toc-text">
          字符拼接</span></a></li>
                                    <li class="toc-item toc-level-3"><a class="toc-link"
                                                                        href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%86%E5%89%B2"><span
                                            class="toc-text">
          字符串分割</span></a></li>
                                </ol>
                            </li>
                            <li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%AD%98"><span
                                    class="toc-text">
          缓存</span></a></li>
                            <li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span
                                    class="toc-text">
          总结</span></a></li>
                            <li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A2%E9%98%85"><span
                                    class="toc-text">
          订阅</span></a></li>
                        </ol>
                    </section><!-- ov = overview-->
                    <div class="sidebar-reading">
                        <div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了</span><span
                                class="sidebar-reading-info__num">6</span><span
                                class="sidebar-reading-info__perc">%</span></div>
                        <div class="sidebar-reading-line" style="transform: translateX(-94%);"></div>
                    </div>
                </aside>
            </div>
            <div class="clearfix"></div>
        </div>
    </main>
    <footer class="footer" id="footer">
        <div class="footer-inner">
            <div><span>Copyright © 2020</span><span class="footer__icon"><i
                    class="fas fa-mug-hot"></i></span><span>未读代码</span><span class="footer__devider">|</span><span>皖ICP备20000567号-1</span>
            </div>
            <div><span color="#ddd">吾生也有涯，而知也无涯。以有涯随无涯，殆已！</span></div>
        </div>
    </footer>
    <div class="loading-bar" id="loading-bar">
        <div class="loading-bar__progress"></div>
    </div>
    <div class="back2top back2top--hide" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span>
    </div>
</div>
<div class="search-mask"></div>
<div class="search-popup"><span class="search-close"></span>
    <div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div>
    <div class="search-results"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script>
<script src="js/utils.js?v=2.1.1"></script>
<script src="js/stun-boot.js?v=2.1.1"></script>
<script src="js/scroll.js?v=2.1.1"></script>
<script src="js/header.js?v=2.1.1"></script>
<script src="js/sidebar.js?v=2.1.1"></script>

</body>
</html>
